package com.memoir.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtils {
    private static final String KEY = "puzzled_kangkang";
    private static final String CLAIMS = "claims";

    // 接受业务数据，生成 token 并返回
    public static String getToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim(CLAIMS, claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 12))
                .sign(Algorithm.HMAC256(KEY));
    }

    // 接受 token，验证 token，并返回业务
    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token)
                .getClaim(CLAIMS)
                .asMap();
    }
}
